package com.springboot.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;



@Component
@Aspect
public class HttpAspect {
    private final  static Logger logger=LoggerFactory.getLogger(HttpAspect.class);
    @Pointcut("execution(public * com.springboot.controller.userController.*( ..))")//写一个公用方法
    public void log(){
    }
    @Before("log()")
    public void dobefore(JoinPoint joinPoint){
        ServletRequestAttributes attributes=(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=attributes.getRequest();
        logger.info("url={}",request.getRequestURL());//获取url
        logger.info("method={}",request.getMethod());//获取metho
        logger.info("ip={}",request.getRemoteAddr());//获取ip
        logger.info("class_method={}",joinPoint.getSignature().getDeclaringTypeName()+
                ","+joinPoint.getSignature().getName());//获取类名和类方法
        logger.info("arg={}",joinPoint.getArgs());//获取参数
    }
    @After("log()")
    public void after(){
       logger.info("222222");
    }
    @AfterReturning(returning = "object",pointcut =  "log()")
    public  void  doafterreturning(Object object){
       logger.info("result={}",object.toString());
    }
}
